<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Area;//地区模型
use app\admin\model\Banks;//银行模型

class Prefecture extends Auth
{
    //地区管理
    public function area(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            //返回公司类型的数据
            $area = new Area();
            $getarea = $area->getarea($res);
            echo json_encode(array('getarea' => $getarea['data'], 'size' => $getarea['count'], 'ids' => $getarea['ids']));
        } elseif ($request->isGet()) {
            return view('area');
        }
    }

    //国家添加
    public function countryadd(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            return view('countryinfo', [
                'str' => 'countryadd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'countryadd') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //国家的编辑
    public function countryedit(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            $info = getinfo($data['id'], 'country');
            return view('countryinfo', [
                'str' => 'countryedit',
                'info' => $info,
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'countryedit') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //省份添加
    public function provincesadd(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            $country = getables('country');
            return view('provincesinfo', [
                'str' => 'provincesadd',
                'country' => $country,
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'provincesadd') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //省份的编辑
    public function provincesedit(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            $country = getables('country');
            $info = getinfo($data['id'], 'provinces');
            return view('provincesinfo', [
                'str' => 'provincesedit',
                'info' => $info,
                'country' => $country,
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'provincesedit') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //城市的添加
    public function cityadd(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            $country = getables('country');
            return view('cityinfo', [
                'str' => 'cityadd',
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'cityadd') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //城市的编辑
    public function cityedit(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            $country = getables('country');
            $info = getinfo($data['id'], 'city');
            $info['country_id'] = getinfo($info['province_id'], 'provinces')['country_id'];
            return view('cityinfo', [
                'str' => 'cityedit',
                'info' => $info,
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'cityedit') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //乡镇的添加
    public function townadd(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            $country = getables('country');
            return view('towninfo', [
                'str' => 'townadd',
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'townadd') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //乡镇的编辑
    public function townedit(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isGet()) {
            $country = getables('country');
            $info = getinfo($data['id'], 'town');
            $cityinfo = getinfo($info['city_id'], 'city');//城市的信息
            $proinfo = getinfo($cityinfo['province_id'], 'provinces');
            $info['province_id'] = $cityinfo['province_id'];
            $info['country_id'] = $proinfo['country_id'];
            return view('towninfo', [
                'str' => 'townedit',
                'country' => $country,
                'info' => $info
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'townedit') {
            $arearet = $area->addeditarea($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //国家的删除
    public function countrydel(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isPost() && $request->isAjax()) {
            $arearet = $area->counprocitown($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //省份的删除
    public function proesdel(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isPost() && $request->isAjax()) {
            $arearet = $area->counprocitown($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //城市的删除
    public function citydel(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isPost() && $request->isAjax()) {
            $arearet = $area->counprocitown($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //城镇的删除
    public function towndel(Request $request)
    {
        $data = $request->param();
        $area = new Area();
        if ($request->isPost() && $request->isAjax()) {
            $arearet = $area->counprocitown($data);
            $udata["status"] = $arearet['status'];
            $udata['msg'] = $arearet['msg'];
            echo json_encode($udata);
        }
    }

    //银行的列表
    public function banks(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            $banks = new Banks();
            $getbanks = $banks->getbanks($res);
            echo json_encode(array('getbanks' => $getbanks['data'], 'size' => $getbanks['count']));
        } elseif ($request->isGet()) {
            return view('banks/banks');
        }
    }

    //银行的添加
    public function banksadd(Request $request)
    {
        $data = $request->param();
        $banks = new Banks();
        if ($request->isGet()) {
            $country = getables('country');
            return view('banks/banksinfo', [
                'str' => 'banksadd',
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'banksadd') {
            $banksret = $banks->addeditbanks($data);
            $udata["status"] = $banksret['status'];
            $udata['msg'] = $banksret['msg'];
            echo json_encode($udata);
        }
    }

    //银行的编辑
    public function banksedit(Request $request)
    {
        $data = $request->param();
        $banks = new Banks();
        if ($request->isGet()) {
            $info = getinfo($data['id'], 'banks');
            $country = getables('country');
            return view('banks/banksinfo', [
                'str' => 'banksedit',
                'info' => $info,
                'country' => $country
            ]);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'banksedit') {
            $banksret = $banks->addeditbanks($data);
            $udata["status"] = $banksret['status'];
            $udata['msg'] = $banksret['msg'];
            echo json_encode($udata);
        }

    }

    //银行的删除
    public function banksdel(Request $request)
    {
        $data = $request->param();
        $banks = new Banks();
        if ($request->isPost() && $request->isAjax()) {
            $banksret = $banks->delbanks($data);
            $udata["status"] = $banksret['status'];
            $udata['msg'] = $banksret['msg'];
            echo json_encode($udata);
        }
    }
}